package com.zhiguang.cms.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zhiguang.cms.entity.Article;
import com.zhiguang.cms.entity.Category;
import com.zhiguang.cms.entity.Comment;
import com.zhiguang.cms.entity.User;
import com.zhiguang.cms.service.ArticleService;
import com.zhiguang.cms.utils.CmsContant;

@RequestMapping("article")
@Controller
public class ArticleController {
	
	@Autowired
	ArticleService articleSerice;
	
	/**
	 * 
	 * @param channelId
	 * @return
	 */
	@RequestMapping("listCatByChannel")
	@ResponseBody
	public List<Category> getCat(int channelId){
		// 根据栏目id获取分类的列表 用户联动
		return articleSerice.listCat(channelId);
		
	}
	
	@RequestMapping("detail")
	public String getDetail(HttpServletRequest request, int id){
		// 根据栏目id获取分类的列表 用户联动
		Article article = articleSerice.getById(id);
		request.setAttribute("article", article);
		return "article";
		
	}
	
	@RequestMapping("addComment")
	@ResponseBody
	public String addComment(HttpServletRequest request, int id,String content) {
		User loginUser = (User) request.getSession().getAttribute(CmsContant.USER_KEY);
		if(loginUser==null) {
			return "用户尚未登陆";
		}
		int result = articleSerice.addComment(id,content,loginUser.getId());
		if(result<=0)
			return "不明原因失败，请稍后再试";
		
		return "success";
		
	}
	
	/**
	 * 根据文章id 显示文章的评论  评论是局部刷新的 也就是文章详情页面中的一部分
	 * @param request
	 * @param id
	 * @param page
	 * @return
	 */
	@RequestMapping("listComment")
	public String listComment(HttpServletRequest request,int id,@RequestParam(defaultValue="1") int page) {
		 /**
		  * 根据文章id获取评论
		  */
		 PageInfo<Comment> commentPage=articleSerice.listComment(id,page);
		 request.setAttribute("commentPage", commentPage);
		 return "comments";
	}
}
